//===============================================================================
// Microsoft patterns & practices
// Web Client Software Factory
//-------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//-------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Collections;

namespace WCSFContrib.PageFlow.WorkflowFoundation
{
	/// <summary>
	/// This service allows data to be passed between Workflow 
	/// and ASP.NET
	/// </summary>
	[Serializable]
	public class DataExchangeService : IDataExchangeService
	{
		/// <summary>
		/// Occurs when data is set.
		/// </summary>
		public event EventHandler<SetDataEventArgs> SetData;

		/// <summary>
		/// Occurs when data is requested.
		/// </summary>
		public event EventHandler<DataKeyEventArgs> GetData;

		/// <summary>
		/// Occurs when data is disposed.
		/// </summary>
		public event EventHandler<DataKeyEventArgs> DisposeData;

		/// <summary>
		/// Occurs when data is returned.
		/// </summary>
		public event EventHandler<DataReturnedEventArgs> DataReturned;

		/// <summary>
		/// Occurs when keys are requested.
		/// </summary>
		public event EventHandler<DataKeyEventArgs> GetKeys;

		/// <summary>
		/// Occurs when keys are returned.
		/// </summary>
		public event EventHandler<KeysReturnedEventArgs> KeysReturned;

		/// <summary>
		/// Fires the DataReturned event.
		/// </summary>
		/// <param name="instanceId">The workflow instance identifier.</param>
		/// <param name="value">The value returned.</param>
		public void FireDataReturned(Guid instanceId, object value)
		{
			if (DataReturned != null)
				DataReturned(this, new DataReturnedEventArgs(instanceId, value));
		}

		/// <summary>
		/// Fires the SetData event.
		/// </summary>
		/// <param name="instanceId">The workflow instance identifier.</param>
		/// <param name="key">The key to set data for.</param>
		/// <param name="value">The data to set.</param>
		public void Set(Guid instanceId, string key, object value)
		{
			if (SetData != null)
				SetData(this, new SetDataEventArgs(instanceId, key, value));
		}

		/// <summary>
		/// Fires the GetData
		/// </summary>
		/// <param name="instanceId">The workflow instance identifier.</param>
		/// <param name="key">The key to get data for.</param>
		public void Get(Guid instanceId, string key)
		{
			if (GetData != null)
				GetData(this, new DataKeyEventArgs(instanceId, key));
		}

		/// <summary>
		/// Fires the DisposeData event
		/// </summary>
		/// <param name="instance">The workflow instance identifier.</param>
		/// <param name="key">The key to remove.</param>
		public void Remove(Guid instance, string key)
		{
			if (DisposeData != null)
				DisposeData(this, new DataKeyEventArgs(instance, key));
		}

		/// <summary>
		/// Fires the GetKeys event.
		/// </summary>
		/// <param name="instance">The workflow instance identifier.</param>
		public void Keys(Guid instance)
		{
			if (GetKeys != null)
				GetKeys(this, new DataKeyEventArgs(instance, null));
		}

		/// <summary>
		/// Fires the KeysReturned event.
		/// </summary>
		/// <param name="instanceId">The workflow instance identifier.</param>
		/// <param name="keys">The collection of returned keys.</param>
		public void FireGetKeysReturned(Guid instanceId, ICollection keys)
		{
			if (KeysReturned != null)
				KeysReturned(this, new KeysReturnedEventArgs(instanceId, keys));
		}
	}
}
